////////////////////////////////////////////////////////////////////////////////
/// @file    main.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _MAIN_C_

// Files includes
#include "led.h"
#include "delay.h"
#include "main.h"


////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MAIN_Exported_Constants
/// @{

float gTrim = 0.99;
s32 gUartBaudRate = 9600;
s32 gNewBaudRate = 0;
//Input capture value frequency
extern u16 gTim3Capture;
//Input capture status
extern u8  gCaptureStart;
extern u8  gCaptureEnd;

////////////////////////////////////////////////////////////////////////////////
/// @brief  This function is main entrance.
/// @param  None.
/// @retval  0.
////////////////////////////////////////////////////////////////////////////////
s32 main(void)
{
    //SetSystemClock(emSYSTICK_On, NULL);
    uart_nvic_init(9600);
    //Input capture
    TIM3_capture_INIT(0xffffffff, 0);
    while(1) {
        //End of TIM3 capture
        if(gCaptureStart && (gCaptureEnd == 0)) {
            gCaptureStart = 0;
            while (gCaptureStart == 0);
            gNewBaudRate = 48000000 / gTim3Capture;
            if (gNewBaudRate > gUartBaudRate * 1.03 || gNewBaudRate < gUartBaudRate * 0.97) {
                gUartBaudRate = (s32)(gNewBaudRate * gTrim);
                ChangeBaudRate(gNewBaudRate);
                UART_SendData(UART1, 0x55);
                while(!UART_GetFlagStatus(UART1, UART_FLAG_TXEPT));
            }
            gCaptureEnd = 1;
        }
    }
}

/// @}

/// @}

/// @}
